package com.lmxdawn.api.dnf.job;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.lmxdawn.api.admin.entity.GoldCoin;
import com.lmxdawn.api.admin.service.IGoldCoinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class GoodJob {


    @Resource
    IGoldCoinService goldCoinService;
    //推送方法，每秒执行一次
    @Scheduled(fixedRate = 60*1000)
    public void check() throws InterruptedException {
        log.info("检查商品下架时间任务执行");
        List<GoldCoin> list = goldCoinService.lambdaQuery()
                .eq(GoldCoin::getType, 2)
                .eq(GoldCoin::getStatus, 1).list();
        for (GoldCoin goldCoin : list) {
            if (StrUtil.isNotBlank(goldCoin.getValidHour())){
                int hours = Integer.parseInt(goldCoin.getValidHour());
                // 如果时间超过了有效期 进行下架
                if (DateUtil.between(new Date(), goldCoin.getCreateTime() , DateUnit.HOUR) > hours){
                    goldCoin.setStatus("0");
                    goldCoinService.updateById(goldCoin);
                    log.info("商品：{}，超过有效期{}，进行下架", goldCoin.getId(), hours);
                }
            }
        }
    }
 
}